package com.weds.edpf.core.service;

import com.weds.bean.jwt.JwtUtils;
import com.weds.core.base.BaseService;
import com.weds.core.resp.JsonResult;
import com.weds.edpf.core.entity.*;
import com.weds.edpf.core.mapper.*;
import com.weds.edpf.core.util.IpUtils;
import com.weds.edpf.core.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author zcz
 * @Description 用户档案表管理
 * @Date 2020-06-03
 */
@Service("userServiceNew")
public class UserService extends BaseService {
    /**
     * 账户类型
     */
    @Resource(name = "dtAcTypeMapperNew")
    private DtAcTypeMapper dtAcTypeMapper;
    /**
     * 职务
     */
    @Resource(name = "ttDutyMapperNew")
    private TtDutyMapper ttDutyMapper;
    /**
     * 档案类型
     */
    @Resource(name = "ttLizhiMapperNew")
    private TtLizhiMapper ttLizhiMapper;
    /**
     * 民族
     */
    @Resource(name = "ttNationMapperNew")
    private TtNationMapper ttNationMapper;
    /**
     * 籍贯
     */
    @Resource(name = "ttNativeMapperNew")
    private TtNativeMapper ttNativeMapper;
    /**
     * 人员角色
     */
    @Resource(name = "ttRoleMapperNew")
    private TtRoleMapper ttRoleMapper;
    /**
     * 性别
     */
    @Resource(name = "ttSexMapperNew")
    private TtSexMapper ttSexMapper;
    /**
     * 自定义字段
     */
    @Resource(name = "ttUserListMapperNew")
    private TtUserListMapper ttUserListMapper;
    /**
     * 学历
     */
    @Resource(name = "ttXueliMapperNew")
    private TtXueliMapper ttXueliMapper;
    /**
     * 用户账户定义
     */
    @Resource
    private AcUserMapper acUserMapper;
    /**
     * 模块
     */
    @Resource
    private WtModuleMapper wtModuleMapper;
    /**
     * 用户账户
     */
    @Resource
    private AcLinkMapper acLinkMapper;
    /**
     * HttpServletRequest
     */
    @Resource
    private HttpServletRequest request;
    /**
     * 档案管理日志
     */
    @Resource
    private UserLogMapper userLogMapper;
    /**
     * 卡
     */
    @Resource(name = "cardMapperNew")
    private CardMapper cardMapper;
    /**
     * 增量
     */
    @Resource
    private WtPublicMapper wtPublicMapper;
    /**
     * 人员管理
     */
    @Resource(name = "userMapperNew")
    private UserMapper userMapper;
    /**
     * 人员场所
     */
    @Resource
    private AcDepUserMapper acDepUserMapper;
    /**
     * 部门审批人
     */
    @Resource(name = "depCustMapperNew")
    private DepCustMapper depCustMapper;
    /**
     * 部门
     */
    @Resource
    private DepMapper depMapper;
    /**
     * 用户调动日志
     */
    @Resource
    private LinkUserchangeLogMapper linkUserchangeLogMapper;
    /**
     * 日志
     */
    @Resource
    private WxUpdateuserLogMapper wxUpdateuserLogMapper;

    private Logger log = LogManager.getLogger();

    /**
     * 查询人员序号并 迭代+1
     * @return
     */
    public String selectModuleUser() {
        String moduleUser = wtModuleMapper.selectModuleUser();
        wtModuleMapper.updateModuleUserIteration();
        return moduleUser;
    }
    /**
     * 前台显示字段
     * @return
     */
    public Map<String, List<Map<String, String>>> displayFields(){
        // 账户类型
        List<Map<String, String>> acTypes = dtAcTypeMapper.acTypeSelect();
        // 职务
        List<Map<String, String>> dutys = ttDutyMapper.dutySelect();
        // 档案类型
        List<Map<String, String>> lizhis = ttLizhiMapper.lizhiSelect();
        // 民族
        List<Map<String, String>> nations = ttNationMapper.nationSelect();
        // 籍贯
        List<Map<String, String>> natives = ttNativeMapper.nativeSelect();
        // 人员角色
        List<Map<String, String>> roles = ttRoleMapper.roleSelect();
        // 性别
        List<Map<String, String>> sexs = ttSexMapper.sexSelect();
        // 查询自定义字段
        List<Map<String, String>> userLists = ttUserListMapper.userlistSelect();
        // 学历
        List<Map<String, String>> xueLis = ttXueliMapper.xueLiSelect();
        Map<String, List<Map<String, String>>> map = new HashMap<>();
        map.put("acTypes",acTypes);
        map.put("dutys",dutys);
        map.put("lizhis",lizhis);
        map.put("nations",nations);
        map.put("natives",natives);
        map.put("roles",roles);
        map.put("sexs",sexs);
        map.put("userLists",userLists);
        map.put("xueLis",xueLis);
        return map;
    }

    /**
     * 生成员工账户号  16位整数组成年月日时分秒+2位随机数
     * @return
     */
    private String createAcNo(){
        Date dd=new Date();
        //格式化
        DateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sim.format(dd);;
        String replace = StringUtils.replace(time, "-", "")
                .replace(":","")
                .replace(" ","");
        String random = String.format("%02d", new Random().nextInt(99));
        return replace + random;
    }

    /**
     * 检查员工账户号是否存在  存在则重新生成
     * @return
     */
    private String checkAcNo(){
        String acNo = createAcNo();
        int acNoCount = userMapper.getCountByAcNo(acNo);
        if(acNoCount > 0){
            checkAcNo();
        }
        return acNo;
    }

    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> insertSelective(UserEntity record) {
        // 获取真实IP
        String ip = IpUtils.getIpAddr(request);
        String gly = null;
        if (JwtUtils.getJwtData(request) != null) {
            gly = JwtUtils.getJwtData(request).getString("userSerial");
        }
        // 根据身份证是否重复 	user_id:身份证号字段
        int userIdCount = userMapper.getCountByUserId(record.getUserId());
        if(userIdCount > 0){
            return failMsg("身份证号已存在!");
        }
        // 工号验证
        if(StringUtils.isEmpty(record.getUserNo())){
            return failMsg("工号不能为空!");
        }
        int countByUserNoAndUserSerial = userMapper.getCountByUserNoAndUserSerial(record.getUserNo(), record.getUserSerial());
        if(countByUserNoAndUserSerial > 0){
            return failMsg("工号已存在!");
        }
        // 查询部门
        Long userDep = null == record.getUserDep() ? null : Long.parseLong(record.getUserDep() +"");
        DepEntity depEntity = depMapper.selectByPrimaryKey(userDep);
        if(Objects.isNull(depEntity)){
            return failMsg("部门不存在!");
        }
        record.setDepNo(depEntity.getDepNo());
        record.setUserDepname(depEntity.getDepName());
        // 查询逻辑卡号
        Integer card = wtModuleMapper.getModuleCard();
        // 卡号迭代 +1
        wtModuleMapper.updateModuleCardIteration();
        String moduleCard = String.format("%08d", card);
        // 生成并检查员工账户号
        String acNo = checkAcNo();
        record.setUserAc(1);
        record.setUserFinger("0000000000");
        record.setUserPhoto(0);
        record.setUserCard(moduleCard);
        record.setUserSj(new Date());
        record.setKqTiaoxiu(0);
        // 默认0不管理考勤机
        Integer userLevel = null == record.getUserLevel() ? 0 : record.getUserLevel();
        record.setUserLevel(userLevel);
        userMapper.insertSelective(record);
        // 判断是否勾选审批人 勾选的话需要新增新增审批人表
        if("1".equals(record.getIsApprover())){
            DepCustEntity depCustEntity = new DepCustEntity();
            depCustEntity.setDepSerial(record.getUserDep());
            depCustEntity.setUserSerial(record.getUserSerial());
            depCustEntity.setGlyNo(gly);
            depCustEntity.setSj(new Date());
            depCustMapper.insertSelective(depCustEntity);
        }
        // 新增员工账户定义表
        AcUserEntity acUser = new AcUserEntity();
        acUser.setAcNo(acNo);
        acUser.setAcType(record.getAcType());
        acUser.setUserSerial(record.getUserSerial());
        acUser.setAcOrder(0);
        acUser.setAcMoney(0);
        acUser.setAcAddm(0);
        acUser.setAcAddo(0);
        acUser.setAcSubm(0);
        acUser.setAcSubo(0);
        acUser.setAcRegm(0);
        acUser.setAcMake(0);
        acUser.setAcPass("123456");
        acUser.setAcKssj(new Date());
        acUser.setAcJssj(new Date());
        acUser.setAcState(0);
        acUser.setSj(new Date());
        acUser.setGlyNo(gly);
        acUser.setAcOverdraw(0);
        acUser.setAcEachm(0);
        acUser.setAcEacho(0);
        acUserMapper.insertSelective(acUser);
        //新增场所与人员关系表
        AcDepUserEntity acDepUserEntity = new AcDepUserEntity();
        acDepUserEntity.setDepSerial(record.getUserDep());
        acDepUserEntity.setDepLname(record.getUserDepname());
        acDepUserEntity.setPerLx(0);
        acDepUserEntity.setSj(new Date());
        acDepUserEntity.setUserSerial(record.getUserSerial());
        acDepUserEntity.setModuleId("0009");
        acDepUserEntity.setGlyNo(gly);
        acDepUserMapper.insertSelective(acDepUserEntity);
        // 新增人员账户表
        AcLinkEntity acLink = new AcLinkEntity();
        acLink.setUserSerial(record.getUserSerial());
        acLink.setAcMoney(BigDecimal.ZERO);
        acLink.setAcAddm(BigDecimal.ZERO);
        acLink.setAcAddo(BigDecimal.ZERO);
        acLink.setAcSubm(BigDecimal.ZERO);
        acLink.setAcSubo(BigDecimal.ZERO);
        acLink.setAcRegm(BigDecimal.ZERO);
        acLink.setAcMake(BigDecimal.ZERO);
        acLink.setAcBlocks(BigDecimal.ZERO);
        acLink.setAcEachm(0);
        acLink.setAcEacho(0);
        acLink.setAcBlockm(BigDecimal.ZERO);
        acLink.setJlCount(0);
        acLink.setAcState(0);
        acLink.setSj(new Date());
        acLink.setGlyNo(gly);
        acLink.setCardSerial(moduleCard);
        acLink.setCardHao(record.getUserSerial()+"");
        acLinkMapper.insertSelective(acLink);
        // 新增卡表
        CardEntity cardEntity = new CardEntity();
        cardEntity.setLx(0);
        cardEntity.setGlyNo(gly);
        cardEntity.setSj(new Date());
        cardEntity.setUserSerial(record.getUserSerial());
        cardEntity.setCardLx(record.getUserLx());
        cardEntity.setCardType(0);
        cardEntity.setCardHao(record.getUserSerial()+"");
        cardEntity.setCardOrder(0);
        cardEntity.setCardSerial(moduleCard);
        cardEntity.setLossCount(0);
        cardEntity.setCardLx(23);
        cardEntity.setIscardreplace(0);
        cardEntity.setIsVirtualCard("1");
        cardEntity.setCardSerial(moduleCard);
        cardMapper.insertSelective(cardEntity);
        // 新增操作日志表
        UserLogEntity userLog = new UserLogEntity();
        userLog.setLx(0);
        userLog.setLogType(0);
        userLog.setLogState(0);
        userLog.setModuleId("0009");
        userLog.setLogBz("新增用户：姓名:"+record.getUserLname()+"工号:"+record.getUserNo());
        userLog.setLogIp(ip);
        userLog.setGlyNo(gly);
        userLogMapper.insertSelective(userLog);
        // 新增增量表,用于下发设备
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(1);
        publicEntity.setIsAll(0);
        publicEntity.setCardSerial(Long.parseLong(moduleCard) );
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(gly);
        publicEntity.setUserSerial(record.getUserSerial());
        wtPublicMapper.insertSelective(publicEntity);
        return succMsg();
    }

    public UserEntity selectByPrimaryKey(Long userSerial) {
        return userMapper.selectByPrimaryKey(userSerial);
    }

    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> updateByPrimaryKeySelective(UserEntity record) {String gly = null;
        if (JwtUtils.getJwtData(request) != null) {
            gly = JwtUtils.getJwtData(request).getString("userSerial");
        }
        UserEntity userEntity = userMapper.selectByPrimaryKey(record.getUserSerial());
        if(Objects.isNull(userEntity)){
            return failMsg("未查到人员信息!");
        }
        // 判断工号身份证号唯一
        int userIdCount = userMapper.getCountByUserIdAndUserSerial(record.getUserId(), record.getUserSerial());
        if(userIdCount > 0){
            return failMsg("身份证号已存在!");
        }
        int userNoCount = userMapper.getCountByUserNoAndUserSerial(record.getUserNo(), record.getUserSerial());
        if(userNoCount > 0){
            return failMsg("工号已存在");
        }
        // 获取真实IP
        String ip = IpUtils.getIpAddr(request);
        userMapper.updateByPrimaryKeySelective(record);
        //根据前台选择的人员账户类型修改员工账户定义表
        acUserMapper.updateAcType(record.getAcType(),record.getUserSerial()+"");
        //删除场所与人员关系表
        userMapper.deleteAcDepUser(record.getModuleId(),record.getUserSerial()+"");
        //新增场所与人员关系表
        AcDepUserEntity acDepUserEntity = new AcDepUserEntity();
        acDepUserEntity.setDepSerial(record.getUserDep());
        acDepUserEntity.setDepLname(record.getUserDepname());
        acDepUserEntity.setPerLx(0);
        acDepUserEntity.setSj(new Date());
        acDepUserEntity.setUserSerial(record.getUserSerial());
        acDepUserEntity.setModuleId(record.getModuleId());
        acDepUserEntity.setGlyNo(gly);
        acDepUserEntity.setModuleId("0009");
        acDepUserMapper.insertSelective(acDepUserEntity);
        // 新增操作日志表
        UserLogEntity userLog = new UserLogEntity();
        userLog.setLx(0);
        userLog.setLogType(0);
        userLog.setLogState(0);
        userLog.setModuleId("0009");
        userLog.setLogBz("修改人员信：息姓名:"+record.getUserLname()+"工号:"+record.getUserNo());
        userLog.setLogIp(ip);
        userLog.setGlyNo(gly);
        userLogMapper.insertSelective(userLog);
        // 新增增量表,用于下发设备
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(1);
        publicEntity.setIsAll(0);
        Long cardSerial = cardMapper.getCardSerialByUserSerial(record.getUserSerial() + "");
        publicEntity.setCardSerial(cardSerial);
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(gly);
        publicEntity.setUserSerial(record.getUserSerial());
        wtPublicMapper.insertSelective(publicEntity);
        // 判断是否勾选审批人 勾选的话需要新增新增审批人表 未勾选则删除
        if("1".equals(record.getIsApprover())){
            DepCustEntity depCustEntity = new DepCustEntity();
            depCustEntity.setDepSerial(record.getUserDep());
            depCustEntity.setUserSerial(record.getUserSerial());
            depCustEntity.setGlyNo(gly);
            depCustEntity.setSj(new Date());
            depCustMapper.insertSelective(depCustEntity);
        }else {
            depCustMapper.deleteByUserSerialAndDepSerial(record.getUserSerial()+"",record.getUserDep()+"");
        }
        return succMsg();
    }

    /**
     * 调动
     * @param depSerial
     * @param userSerial
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> transfer(String depSerial,String userSerial){
        String gly = UserUtils.currentGly(request);
        UserEntity user = userMapper.selectByPrimaryKey(Long.parseLong(userSerial + ""));
        if(Objects.isNull(user)){
            return failMsg("未查到人员信息!");
        }
        // 查询部门
        DepEntity depEntity = depMapper.selectByPrimaryKey(Long.parseLong(depSerial));
        if(Objects.isNull(depEntity)){
            return failMsg("部门不存在");
        }
        // 旧的部门编号
        Long oldUserDep = null == user.getUserDep()?null:Long.parseLong(user.getUserDep()+"");
        // 获取真实IP
        String ip = IpUtils.getIpAddr(request);
        // 删除部门审批人记录
        depCustMapper.deleteByUserSerialAndDepSerial(depSerial+"",userSerial);
        // 修改人员信息
        UserEntity userEntity = new UserEntity();
        userEntity.setDepNo(depEntity.getDepNo());
        userEntity.setUserDep(Integer.parseInt(depSerial));
        userEntity.setUserDepname(depEntity.getDepName());
        userEntity.setUserSj(new Date());
        userEntity.setUserSerial(Long.parseLong(userSerial));
        userMapper.updateByPrimaryKeySelective(userEntity);
        // 修改消费记录表中的部门序号
        userMapper.updateXfMxDepSerial(depSerial,userSerial);
        // 新增操作日志
        LinkUserchangeLogEntity linkUserchangeLogEntity = new LinkUserchangeLogEntity();
        linkUserchangeLogEntity.setGlyNo(gly);
        linkUserchangeLogEntity.setNewDep(Long.parseLong(depSerial));
        Long userDep = null == user.getUserDep()?null:Long.parseLong(user.getUserDep()+"");
        linkUserchangeLogEntity.setOldDep(oldUserDep);
        linkUserchangeLogEntity.setSj(new Date());
        linkUserchangeLogEntity.setUserSerial(Long.parseLong(userSerial));
        linkUserchangeLogMapper.insertSelective(linkUserchangeLogEntity);
        // 增量表,用于下发设备
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(9);
        publicEntity.setIsAll(0);
        Long cardSerial = cardMapper.getCardSerialByUserSerial(userSerial);
        publicEntity.setCardSerial(cardSerial);
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(gly);
        publicEntity.setUserSerial(Long.parseLong(userSerial));
        publicEntity.setNewNumber(Long.parseLong(depSerial));
        publicEntity.setOldNumber(Long.parseLong(user.getUserDep()+""));
        wtPublicMapper.insertSelective(publicEntity);
        // 新增操作日志
        UserLogEntity userLogEntity = new UserLogEntity();
        userLogEntity.setLogBz("我是："+user.getUserLname()+"工号："+user.getUserNo()+"从"+user.getUserDepname()+"调动到"+depEntity.getDepName());
        userLogEntity.setLx(4);
        userLogEntity.setLogType(0);
        userLogEntity.setLogState(0);
        userLogEntity.setModuleId(user.getModuleId());
        userLogEntity.setLogIp(ip);
        userLogEntity.setGlyNo(gly);
        userLogEntity.setLogSj(new Date());
        userLogMapper.insertSelective(userLogEntity);
        return succMsg();
    }


    /**
     * 离职
     * @param userSerial
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> quit(String userSerial){
        UserEntity user = userMapper.selectByPrimaryKey(Long.parseLong(userSerial));
        if(Objects.isNull(user)){
            return failMsg("未查到人员信息!");
        }
        if(51 == user.getUserType()){
            return failMsg("员工已为离职状态!");
        }
        // 获取真实IP
        String ip = IpUtils.getIpAddr(request);
        String gly = UserUtils.currentGly(request);
        // 修改人员状态为离职.
        UserEntity userEntity = new UserEntity();
        userEntity.setUserSerial(Long.parseLong(userSerial));
        userEntity.setUserSj(new Date());
        userEntity.setUserType((short) 51);
        userMapper.updateByPrimaryKeySelective(userEntity);
        // 新增操作日志
        UserLogEntity userLogEntity = new UserLogEntity();
        userLogEntity.setLogBz("我是："+user.getUserLname()+"工号："+user.getUserNo()+"离职");
        userLogEntity.setLx(3);
        userLogEntity.setLogType(0);
        userLogEntity.setLogState(0);
        userLogEntity.setModuleId(user.getModuleId());
        userLogEntity.setLogIp(ip);
        userLogEntity.setGlyNo(gly);
        userLogEntity.setLogSj(new Date());
        userLogMapper.insertSelective(userLogEntity);
        // 删除人员微信绑定关系, 用于移动端登录
        userMapper.deleteWxLoginOpenidByUserSerial(userSerial);
        WxUpdateuserLog wxUpdateuserLog = new WxUpdateuserLog();
        wxUpdateuserLog.setUserSerial(userSerial);
        wxUpdateuserLog.setResult("离职");
        wxUpdateuserLog.setLogSj(new Date());
        wxUpdateuserLog.setLogIp(ip);
        wxUpdateuserLog.setResult(gly);
        wxUpdateuserLogMapper.insertSelective(wxUpdateuserLog);
        // 新增人员增量,用于下发设备
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(3);
        publicEntity.setIsAll(0);
        Long cardSerial = cardMapper.getCardSerialByUserSerial(userSerial);
        publicEntity.setCardSerial(cardSerial);
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(gly);
        publicEntity.setUserSerial(Long.parseLong(userSerial));
        wtPublicMapper.insertSelective(publicEntity);
        return succMsg();
    }

    /**
     * 查询用户信息
     * @param record
     * @return
     */
    public List<UserEntity> selectListByEntity(UserEntity record) {
        List<UserEntity> list = userMapper.selectListByEntity(record);
        // 列表仅展示字段转中文
        list.forEach(u -> {
            // 消费权限组
            String acName = userMapper.getAcNameByUserSerial(u.getUserSerial() + "");
            u.setAcTypeName(acName);
            // 部门审批人  汇总部门审批人表
            Integer depCustCount = userMapper.getDepCustCountByUserSerial(u.getUserSerial() + "");
            if(depCustCount > 0){
                u.setIsApprover("是");
            }else{
                u.setIsApprover("否");
            }
        });
        return list;
    }

    /**
     * 基本信息 人员编辑界面
     * @param userSerial
     * @return
     */
    public Map<String,String> getUserBasicInfoByUserSerial(String userSerial){
        return userMapper.getUserBasicInfoByUserSerial(userSerial);
    }


    /**
     * 员工照片 人员编辑界面
     * @param userSerial
     * @return
     */
    public DtPhotoEntity getPhoto(String userSerial){
        return userMapper.getPhoto(userSerial);
    }

    /**
     * 复职
     * @param userSerial
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> reinstatement(String userSerial){
        UserEntity user = userMapper.selectByPrimaryKey(Long.parseLong(userSerial));
        if(Objects.isNull(user)){
            return failMsg("未查到用户信息");
        }
        if(51 != user.getUserType()){
            return failMsg("员工非离职状态无法复职!");
        }
        userMapper.updateUserTypeByUserSerial(0, userSerial);
        // 获取真实ip
        String ip = IpUtils.getIpAddr(request);
        // 查询卡编号
        Long cardSerial = cardMapper.getCardSerialByUserSerial(userSerial);
        // 是否有指纹
        boolean isFinger = StringUtils.contains(user.getUserFinger(), "1");
        if(isFinger){
            // 新增指纹增量
            WtPublicEntity publicEntity = new WtPublicEntity();
            publicEntity.setLx(4);
            publicEntity.setLogType(1);
            publicEntity.setIsAll(0);
            publicEntity.setCardSerial(cardSerial);
            publicEntity.setLogSj(new Date());
            publicEntity.setLogIp(ip);
            publicEntity.setGlyNo(UserUtils.currentGly(request));
            publicEntity.setUserSerial(Long.parseLong(userSerial));
            wtPublicMapper.insertSelective(publicEntity);
        }
        // 新增用户增量
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(10);
        publicEntity.setIsAll(0);
        publicEntity.setCardSerial(cardSerial);
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(UserUtils.currentGly(request));
        publicEntity.setUserSerial(Long.parseLong(userSerial));
        wtPublicMapper.insertSelective(publicEntity);
        // 新增操作日志表
        UserLogEntity userLog = new UserLogEntity();
        userLog.setLx(5);
        userLog.setLogType(0);
        userLog.setLogState(0);
        userLog.setModuleId(user.getModuleId());
        userLog.setLogBz("复职：姓名:"+user.getUserLname()+"工号:"+user.getUserNo());
        userLog.setLogIp(ip);
        userLog.setGlyNo(UserUtils.currentGly(request));
        userLogMapper.insertSelective(userLog);
        return succMsg();
    }

    /**
     * 离职调动
     * @param depSerial
     * @param userSerial
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Object> quitTransfer(String depSerial,String userSerial){
        String gly = UserUtils.currentGly(request);
        // 获取真实IP
        String ip = IpUtils.getIpAddr(request);
        UserEntity user = userMapper.selectByPrimaryKey(Long.parseLong(userSerial));
        if(Objects.isNull(user)){
            return failMsg("未查到人员信息!");
        }
        if(51 != user.getUserType()){
            return failMsg("员工非离职状态无法调用!");
        }
        // 查询部门
        DepEntity dep = depMapper.selectByPrimaryKey(Long.parseLong(depSerial));
        if(Objects.isNull(dep)){
            return failMsg("部门不存在");
        }
        // 旧的部门编号
        Long oldUserDep = null == user.getUserDep()?null:Long.parseLong(user.getUserDep()+"");
        // 删除非指定部门审批人记录
        depCustMapper.deleteByUserSerialAndDepSerial(depSerial,userSerial);
        // 修改人员信息
        UserEntity userEntity = new UserEntity();
        userEntity.setDepNo(dep.getDepNo());
        userEntity.setUserDep(Integer.parseInt(depSerial));
        userEntity.setUserDepname(dep.getDepName());
        userEntity.setUserSj(new Date());
        userEntity.setUserType((short) 0);
        userEntity.setUserSerial(Long.parseLong(userSerial));
        userMapper.updateByPrimaryKeySelective(userEntity);
        // 修改消费记录表中的部门序号
        userMapper.updateXfMxDepSerial(depSerial,userSerial);
        // 新增操作日志
        LinkUserchangeLogEntity linkUserchangeLogEntity = new LinkUserchangeLogEntity();
        linkUserchangeLogEntity.setGlyNo(gly);
        linkUserchangeLogEntity.setNewDep(Long.parseLong(depSerial));
        linkUserchangeLogEntity.setOldDep(oldUserDep);
        linkUserchangeLogEntity.setSj(new Date());
        linkUserchangeLogEntity.setUserSerial(Long.parseLong(userSerial));
        linkUserchangeLogMapper.insertSelective(linkUserchangeLogEntity);
        // 增量表,用于下发设备
        WtPublicEntity publicEntity = new WtPublicEntity();
        publicEntity.setLx(1);
        publicEntity.setLogType(10);
        publicEntity.setIsAll(0);
        Long cardSerial = cardMapper.getCardSerialByUserSerial(userSerial);
        publicEntity.setCardSerial(cardSerial);
        publicEntity.setLogSj(new Date());
        publicEntity.setLogIp(ip);
        publicEntity.setGlyNo(gly);
        publicEntity.setUserSerial(Long.parseLong(userSerial));
        publicEntity.setNewNumber(Long.parseLong(depSerial));
        publicEntity.setOldNumber(Long.parseLong(user.getUserDep()+""));
        wtPublicMapper.insertSelective(publicEntity);
        // 新增操作日志
        UserLogEntity userLogEntity = new UserLogEntity();
        userLogEntity.setLogBz("我是："+user.getUserLname()+"工号："+user.getUserNo()+"从"+user.getUserDepname()+"调动到"+dep.getDepName());
        userLogEntity.setLx(4);
        userLogEntity.setLogType(0);
        userLogEntity.setLogState(0);
        userLogEntity.setModuleId(user.getModuleId());
        userLogEntity.setLogIp(ip);
        userLogEntity.setGlyNo(gly);
        userLogEntity.setLogSj(new Date());
        userLogMapper.insertSelective(userLogEntity);
        return succMsg();
    }

}
